
/**
 * 拉起微信支付
 * @param object,微信签名信息
 */
export const  wxPay = async (paySignature) => {
	console.log(paySignature);
	if (typeof WeixinJSBridge == "undefined") {
		if (document.addEventListener) {
			document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		} else if (document.attachEvent) {
			document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
			document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		}
	} else {
		return await onBridgeReady(paySignature);
	}
};

function onBridgeReady(param) {
	return new Promise((resolve, reject) => {
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest', {
				"appId": param.appId, //公众号名称，由商户传入     
				"timeStamp": param.timeStamp, //时间戳，自1970年以来的秒数     
				"nonceStr": param.nonceStr, //随机串     
				"package": param.packageValue,
				"signType": param.signType, //微信签名方式：     
				"paySign": param.paySign //微信签名 
			},
			function(res) {
				if (res.err_msg == "get_brand_wcpay_request:ok") {
					resolve(true);
				} else {
					console.log('支付失败或支付取消');
					reject(false)
				}
			});
	})
};
